<?php
class MasterAction extends Action{

	
	
	public function addMaster(){//【添加师傅】
		$uid = dt($_REQUEST['uid']);
		$master_code = dt($_REQUEST['master_code']);
		
		$this->checkMasterLocal($uid);//检查是否已存在师傅
		$master_id = $this->checkRelationShip($uid,$master_code);//检查对方是否为徒弟
		
		$user = M("User");
		$user->master_id = $master_id;
		$user->uid = $uid;
		
		if($user->where("uid = $uid")->save()){
			$value = ["code" =>"success",'master_id'=>$master_id];
			header("Content-Type: text/json");
			die(et($value));
		}else{
			$value = ["code" =>"false"];
			header("Content-Type: text/json");
			die(et($value));
		}
	}

	public function checkMasterLocal($uid){//【本地检查是否已存在师傅】
		$user = M("User");
		$info = $user->where("uid = $uid")->find();
		if($info['master_id'] && $info['master_id'] != NULL){
			$value = ["code" =>"hadMaster"];
			header("Content-Type: text/json");
			die(et($value));
		}
		
	}
	
	public function checkRelationShip($uid,$master_code){//【检查对方是否为徒弟】
		$user = M("User");
		
		$master = $user->where("master_code = '$master_code'")->find();
		$info = $user->where("uid = $uid")->find();
		if($master['master_id'] == $info['uid'] && $master['master_id'] != NULL){//对方为徒弟
			$value = ["code" =>"isDisciple"];
			header("Content-Type: text/json");
			die(et($value));
		}
		
		return $master['uid'];
	}
	
	public function checkMaster(){//【检查是否已存在师傅】
		$uid = dt($_REQUEST['uid']);
		$user = M("User");
		$info = $user->where("uid = $uid")->find();
		if($info){
			if($info['master_id'] && $info['master_id'] != NULL){
				$value = ["code" =>"hadMaster",'master_id'=>$info['master_id']];
				header("Content-Type: text/json");
				die(et($value));
			}else{
				$value = ["code" =>"noHave"];
				header("Content-Type: text/json");
				die(et($value));
			}
		}else{
			$value = ["code" =>"false"];
			header("Content-Type: text/json");
			die(et($value));
		}
	}
	
	
	public function getMaster(){//【获取师傅或者徒弟列表】
		$uid = dt($_REQUEST['uid']);
		$type = dt($_REQUEST['type']);
		$user = M("User");
		if($type == 'master'){//获取师傅
			$info = $user->where("uid = $uid")->field("master_id")->find();
			$master_id = $info['master_id'];
			$data = $user->where("uid = $master_id")->field("uid,uname,avatar")->find();
			if($data){
				if($data['avatar']){
					$data['avatar'] = SITE_URL.'/'.$data['avatar'];
				}
				$value = ["code" =>"success",'data'=>$data];
				header("Content-Type: text/json");
				die(et($value));
			}else{
				$value = ["code" =>"notfound"];
				header("Content-Type: text/json");
				die(et($value));
			}
		}elseif ($type == 'disciple'){//获取徒弟
			$info = $user->where("master_id = $uid")->field("uid")->select();
			if($info){
				foreach ($info as $in){
					$id = $in['uid'];
					$data[] = $user->where("uid = $id")->field("uid,uname,avatar")->find();
					
				}
				foreach ($data as $key=>$da){
					if($da['avatar']){
						$data[$key]['avatar'] = SITE_URL.'/'.$da['avatar'];
					}
				}
				$value = ["code" =>"success",'data'=>$data];
				header("Content-Type: text/json");
				die(et($value));
			}else{
				$value = ["code" =>"notfound"];
				header("Content-Type: text/json");
				die(et($value));
			
			}
		
		}
			$value = ["code" =>"false"];
			header("Content-Type: text/json");
			die(et($value));
	
	}
	
	
	
	
	
	
	
	
}